import io from '../../utils/socket.io.xcx.min.js'
const socket = io('http://ngrok.chengguo.link/')

Page({
	data: {
		msgs: [],
		text: ''
	},
	onLoad() {
		this.getUserInfo()
		// socket.on('connect', () =>  {
		// 	console.log('connection')
		// })
		this.setData({
			msgs: wx.getStorageSync('msgs') || [],
		})
		socket.on('chat', data => {
			if (data.toUser === this.user.token) {
				this.data.msgs.push(data)
				this.setData({
					msgs: this.data.msgs
				})
			}
		})

		// socket.on('disconnect', msg => {
		// 	console.log(msg)
		// })
		// socket.on('A', data => {
		// 	console.log('A', data)
		// 	this.setData({
		// 		msgs: this.data.msgs.concat([{
		// 			A: data
		// 		}])
		// 	})
		// })
	},
	sendMsg(e) {
		if (this.data.text.trim()) {
			const msg = {
				role: 1,
				user: {
					token: this.user.token,
					avatar: this.user.avatarUrl,
					nickname: this.user.nickName
				},
				time: Date(),
				msg: this.data.text,
			}
			socket.emit('chat', msg)
			this.data.msgs.push(msg)

			this.setData({
				text: '',
				msgs: this.data.msgs
			})
		}
	},

	getUserInfo() {
		if (!this.user) {
			this.user = wx.getStorageSync('userInfo')
			this.user.token = wx.getStorageSync('token')
		}
	},

	getValue(e) {
		this.setData({
			text: e.detail.value
		})
	},

	onUnload() {
		wx.setStorage({
			key: 'msgs',
			data: this.data.msgs,
		})
	}

	// /*
	// createConnect() {
	// 	const socket = (this.socket = io(
	// 		// 'https://vast-plateau-30681.herokuapp.com/',
	// 		'http://localhost:3030'
	// 	))

	// 	/**
	// 	 * Aboud connection
	// 	 */
	// 	socket.on('connect', () => {
	// 		// this.popMessage()
	// 		// this.pushMessage(createSystemMessage('You joined'))
	// 	})

	// 	socket.on('connect_error', d => {
	// 		console.log(`connect_error: ${d}`)
	// 		// this.pushMessage(createSystemMessage(`connect_error: ${d}`))
	// 	})

	// 	socket.on('connect_timeout', d => {
	// 		console.log(`connect_timeout: ${d}`)
	// 		// this.pushMessage(createSystemMessage(`connect_timeout: ${d}`))
	// 	})

	// 	socket.on('disconnect', reason => {
	// 		console.log(`disconnect: ${reason}`)
	// 		// this.pushMessage(createSystemMessage(`disconnect: ${reason}`))
	// 	})

	// 	socket.on('reconnect', attemptNumber => {
	// 		console.log(`reconnect success: ${attemptNumber}`)
	// 		// this.pushMessage(
	// 		// 	createSystemMessage(`reconnect success: ${attemptNumber}`),
	// 		// )
	// 	})

	// 	socket.on('reconnect_failed', () => {
	// 		console.log(reconnect_failed)
	// 		// this.pushMessage(createSystemMessage('reconnect_failed'))
	// 	})

	// 	socket.on('error', err => {
	// 		console.error(`error: ${err}`)
	// 		// this.pushMessage(createSystemMessage(`error: ${err}`))
	// 	})

	// 	/**
	// 	 * About chat
	// 	 */
	// 	socket.on('login', d => { })

	// 	socket.on('new message', d => {
	// 		const { username, message } = d
	// 		this.pushMessage(createUserMessage(message, username))
	// 	})

	// 	socket.on('user joined', d => {
	// 		this.pushMessage(createSystemMessage(`${d.username} joined`))
	// 		this.pushMessage(createSystemMessage(`当前共有 ${d.numUsers} 人`))
	// 	})

	// 	socket.on('user left', d => {
	// 		this.pushMessage(createSystemMessage(`${d.username} left`))
	// 		this.pushMessage(createSystemMessage(`当前共有 ${d.numUsers} 人`))
	// 	})

	// 	socket.on('typing', d => { })

	// 	socket.on('stop typing', d => { })

	// 	socket.emit('add user', this.me.nickName)
	// }
	// */
})